﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;


namespace ApplicationClienteCommunicante
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            l = new ApplicationClienteCommunicante.ClientApplicationCommunicante(this);
        }



        ApplicationClienteCommunicante.ClientApplicationCommunicante l;
        Boolean found;
        int counter;
        static public string nomdispo;


        public void RefreshMessage2(string message)
        {
            textBlock1.Text = message;
            textBlock1.Refresh();
        }



        private void RefreshMessage(string message)
        {
            Message.Text = message;
            Message.Refresh();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            nomdispo = textBox1.Text;
            Console.WriteLine("nomdispo du dispositif est :" + nomdispo);
            


            found = false;
            counter = 0;
            try
            {
                RefreshMessage("Discovering...");

                do
                {

                    l.Search();

                    if (ApplicationClienteCommunicante.ClientApplicationCommunicante.Zones.Count > 0)
                    {

                        found = true;
                        Console.WriteLine("¤¤¤¤¤¤¤¤¤¤¤¤¤¤ true ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
                        RefreshMessage("Discovered " + ApplicationClienteCommunicante.ClientApplicationCommunicante.Zones[0].ToString() + " devices :  " + ApplicationClienteCommunicante.ClientApplicationCommunicante.Zones[1].ToString());
                        counter = 4;
                       

                    }
                    else
                    {
                        found = false;
                        RefreshMessage("Discovering...");
                        Console.WriteLine("¤¤¤¤¤¤¤¤¤¤¤¤¤¤ false ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
                        Console.WriteLine("Sleep for 5 seconds.");
                        Thread.Sleep(5000);
                        counter++;
                    }
                }

                while (counter < 4);

                Console.WriteLine("¤¤¤¤¤¤¤¤¤¤¤¤¤¤ j'ai terminé ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤");
            }

            catch (Exception ex)
            {
                RefreshMessage(ex.Message + " Try discovery again.");
            }

            if (found == false)
            {
                RefreshMessage("No devices discovered. Try discovery again.");
            }


        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Console.WriteLine(textBox2.Text);
            Console.WriteLine(textBox3.Text);
            double m1 = Convert.ToDouble(textBox2.Text);
            double m2 = Convert.ToDouble(textBox2.Text);

            Console.WriteLine(m1);
            Console.WriteLine(m2);

            l.getParametreInterface(m1, m2);
            RefreshMessage2("Parametres sent");

        }


    
    
    }




    public static class ExtensionMethods
    {
        private static Action EmptyDelegate = delegate() { };

        public static void Refresh(this UIElement uiElement)
        {
            uiElement.Dispatcher.Invoke(EmptyDelegate, System.Windows.Threading.DispatcherPriority.Render);
        }
    }
   
}
